Skip to content

New config service#55

Open
mikyas-dev wants to merge 24 commits intodevelopmentfrom
new-config-service
Open

New config service#55
mikyas-dev wants to merge 24 commits intodevelopmentfrom
new-config-service

Conversation

@mikyas-dev
Copy link
Copy Markdown

No description provided.


private loadConfiguration(): IAppConfiguration {
const config = yaml.load(
readFileSync(join(__dirname, './config.yaml'), 'utf8')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid magic strings


const keycloakConfig: IKeycloakConfiguration = configValue;

const local = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does local mean in this context?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it means from local enviroment

private loadConfiguration(): IAppConfiguration {
const config = yaml.load(
readFileSync(join(__dirname, './config.yaml'), 'utf8')
) as Record<string, unknown>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of as Record<string, unknown> here?


const localConfig: ILocalConfiguration = localValue;

return {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it seems that you only load keycloak config from file and the rest from ENV. Why is that?

Copy link
Copy Markdown
Author

@mikyas-dev mikyas-dev Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the keycloak config contain sensitive data? I thought they were not sensitive.

};
}

getKcConfig(): IKeycloakConfiguration {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's really no point in sacrificing readability for the number of characters. getKeycloakConfig is better in this case

@@ -0,0 +1,47 @@
import * as Joi from 'joi';

export interface IKeycloakConfiguration {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not prefix interfaces with I. Homework: do some research online for reasons why. Let's discuss more after you get some insights

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from all i found this 2 make sense
1, Readability and Expressiveness: Some argue that a well-named interface should be clear and expressive on its own without needing a prefix. If the interface name is appropriately descriptive, the 'I' might be redundant.

2, Unnecessarily Revealing Implementation Details: The 'I' prefix might expose the underlying implementation details, which could be considered a violation of the abstraction principle. Interfaces should define a contract without revealing how it will be implemented.

Copy link
Copy Markdown
Contributor

@peterrogov peterrogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Naming for config sections. Needs revisiting.

  2. Logic of merging the config file data with ENV is still a little flawed. See my comments before for this service.

Config YML

keycloak:
  realm: 'humanitech'
  adminPassword: '123456'
  
database:
  host: '127.0.0.1'

ENV file

KC_ADMIN_PASSWORD=123789

Sketch code to merge and validate

const configYmlToEnvMap = {
  "keycloak.adminPassword": "KC_ADMIN_PASSWORD"
  // ... and more as required ...
}

const config = loadYmlFileAndValidate(); // load and validate the config from file

// Override from ENV
Object.entries(TaskProcessor).forEach(([ymlKey, envName]) => {
    if(process.env[envName]) {
       // think of a way how to set a property deep in the tree using its path
       // for example, lodash.set does this job pretty well
        setConfig(config, ymlKey, process.env[envName]);
    }
});

// after overriding from ENV the config could receive invalid values
// one more round of schema based validation is required
const finalConfig = validate(config);

// Now you have a complete, merged, validated config. 

throw new Error(`Config validation error: ${localError.message}`);
for (const key in yamlConfig.local) {
if (process.env[key]) {
yamlConfig.local[key] = process.env[key];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about validation? what if there's garbage in ENV?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of variables that come from the environment (process.env) is always a string, but I used dto, that will handle the validation for the whole object. so if I didn't get you, you can correct me

grantType: 'password'
clientId: 'admin-cli'

local:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why local? what does it mean? if there's a need for sections, why not to make them reasonable, like keycloak, database, etc?

DB_DATABASE: 'mydb'
POSTGRES_DATA: '/path/to/postgres/data'
KEYCLOAK_ADMIN: 'admin-user'
KEYCLOAK_ADMIN_PASSWORD: 'admin-password'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is there a section for keycloak config and yet this parameter is stored here?

@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants